from Simulinux.Views.View import View
from Simulinux.Views.PinView import PinView
from Simulinux.Core.Net import *

__all__ = ['PinView', 'PIN_INPUT', 'PIN_OUTPUT', 'PIN_LENGTH']

TOLERANCE_Y = 3
TOLERANCE_X = 2

class NetView(View):
    def __init__(self, net):
        self.net = net
    def connect_origin_pin(self, pin):
        self.net.set_orig(pin)
    def connect_destination_pin(self, pin):
        self.net.set_dest(pin)
    def connect_origin_point(self, x, y):
        self.net.set_orig((x,y))
    def connect_destination_point(self, x, y):
        self.net.set_dest((x,y))
    def is_my_region(self, x, y):
        return False
        '''xr = x - self.pin.get_block().posx
        yr = y - self.pin.get_block().posy
        if xr > (self.x1 - TOLERANCE_X) and \
           xr < (self.x2 + TOLERANCE_X) and \
           yr > (self.y1 - TOLERANCE_Y) and \
           yr < (self.y2 + TOLERANCE_Y):
            return True
        else:
            return False'''
    def draw(self, context):
        orig = self.net.get_origin()
        dest = self.net.get_dest()
        segments = self.net.get_segments()
        if segments:
            if type(orig) == PinView:
                orig = orig.get_coords()
            context.move_to(orig[0], orig[1])
            for s in segments:
                if s.type == SEGMENT_HORIZONTAL:
                    context.rel_line_to(s.length, 0)
                else:
                    context.rel_line_to(0, s.length)
        else:
            if type(orig) == PinView:
                orig = orig.get_coords()
            if type(dest) == PinView:
                dest = dest.get_coords()
            #@TODO: add automatic orthogonal lines creation!!!
            context.move_to(orig[0], orig[1])
            context.line_to(dest[0], dest[1])
        context.stroke()
    def edit_properties(self):
        pass
